<template>
	<view>
	<u-form :model="form" ref="uForm">
		<u-form-item label="电话号码" prop='phone'><u-input v-model="form.phone" /></u-form-item>
		<u-form-item label="地址" prop="address"p><u-input v-model="form.address" /></u-form-item>
		<u-form-item label="赏金" prop="total_fee"><u-input v-model="form.total_fee" /></u-form-item>
		<u-form-item label="取件码" prop="code"><u-input v-model="form.code" /></u-form-item>
		<u-form-item label="类别">
			<u-radio-group v-model="form.title">
				<u-radio v-for="(item, index) in titleList" :key="index" :name="item.name" :disabled="item.disabled">
					{{ item.name }}
				</u-radio>
			</u-radio-group>
		</u-form-item>
		<u-form-item label="快递公司">
			<u-radio-group v-model="form.type">
				<u-radio v-for="(item, index) in typeList" :key="index" :name="item.name" :disabled="item.disabled">
					{{ item.name }}
				</u-radio>
			</u-radio-group>
		</u-form-item>
		<u-form-item label="备注"><u-input v-model="form.remark" /></u-form-item>
	</u-form>
	<view>
		<u-modal v-model="show" :content="content" :mask-close-able="true" @confirm='clean'></u-modal>
		<u-button @click="submit">发布</u-button>
		
	</view>
	</view>
</template>

<script>
	
export default {
	data() {
		return {
			show:false,
			content: '已发布',
			titleList: [
				{
					name: '大件',
					
				},
				{
					name: '中件',
					
				},
				{
					name: '小件',
					
				}
			],
			typeList: [
				{
					name: '圆通',
					disabled: false
				},
				{
					name: '中通',
					disabled: false
				},
				{
					name: '顺丰',
					disabled: false
				}
			],
			form: {
				phone: '',
				address:'',
				remark:'',
				title: '',
				type:'',
				total_fee:'',
				code:'',
				status:1,
				create_time:'',
			},
			
			rules:{
				phone:[
					{required: true, 
						message: '请输入正确的手机号码', 
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change','blur']},
						{
									// 自定义验证函数，见上说明
									validator: (rule, value, callback) => {
										// 上面有说，返回true表示校验通过，返回false表示不通过
										// this.$u.test.mobile()就是返回true或者false的
										return this.$u.test.mobile(value);
									},
									message: '请输入正确的手机号码',
						}
						],
				address:[{required:true,message:'请输入地址',trigger:['change','blur']}],
				total_fee:[{required:true,message:'请输入赏金',trigger:['change','blur']}],
				code:[{required:true,message:'请输入取件码',trigger:['change','blur']}]
						
			}
				
			
		};
	},
	onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
	methods:{
		submit(){
			this.$refs.uForm.validate(valid => {
							if (valid) {
								var date = new Date(),
								year = date.getFullYear(),
								month = date.getMonth() + 1,
								day = date.getDate(),
								hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
								minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
								second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
								month >= 1 && month <= 9 ? (month = "0" + month) : "";
								day >= 0 && day <= 9 ? (day = "0" + day) : "";
								var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
								this.form.create_time=timer
								const db = uniCloud.database();
								db.collection('uni-id-base-order').add(this.form).then(e=>{
									this.show = true;
								})
								
							} else {
								console.log('验证失败');
							}
						});
			
			
			
			
			
			//创建时间
			// var date = new Date(),
			// year = date.getFullYear(),
			// month = date.getMonth() + 1,
			// day = date.getDate(),
			// hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
			// minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
			// second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
			// month >= 1 && month <= 9 ? (month = "0" + month) : "";
			// day >= 0 && day <= 9 ? (day = "0" + day) : "";
			// var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
			// this.form.create_time=timer
			
			// const db = uniCloud.database();
			// db.collection('uni-id-base-order').add(this.form).then(e=>{
			// 	console.log(e)
			// })
			// this.show = true;
			// console.log(this.create_time)
			
		},
		clean(){
			 uni.navigateTo({
				url:'/pages/list/select/select'
			})
		},
		
	}
	};

</script>
